package com.google.code.p.localspinner;

import java.io.Closeable;
import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.List;

import android.location.Address;
import android.widget.EditText;
/**
 * Application Utilities
 * 
 * @author liangj01
 *
 */
public class AppUtils{
  
    /**
     * Round the input number to <code>decimalPlaces</code>
     * @param val
     * @param decimalPlaces
     * @return
     */
    public static double roundTo(double val, int decimalPlaces) {
        double p = Math.pow(10,decimalPlaces);
        val = val * p;
        float tmp = Math.round(val);
        return tmp/p;
    }
   
   
  public static <T> T nvl(T t, T replacement){
    return t == null ? replacement : t;
  }
  
  /** 
   * @param c
   */
  public static void closeQuietly(Closeable c){
      if(c != null){
        try {
            c.close();
        } catch (IOException e) {}
      }
  }
   
  public static CharSequence[] addressToString(List<Address> addr){
      LinkedHashSet<CharSequence> all = new LinkedHashSet<CharSequence>();
      for(int i =0; i< addr.size(); i++){
        Address a = addr.get(i);
        
        StringBuilder sb = new StringBuilder();
        if(a.getThoroughfare() != null){
          sb.append(a.getThoroughfare()).append(", "); //address
        }
        sb.append(AppUtils.toCszString(AppUtils.nvl(a.getLocality(), a.getFeatureName()), a.getAdminArea(), a.getPostalCode()));

        if(sb.length() == 0){
          for(int j = 0; j< a.getMaxAddressLineIndex(); j++){
            sb.append(a.getAddressLine(j));
            sb.append(j+1 == a.getMaxAddressLineIndex() ? "" : ", ");
          }
        }
        if(sb.length() != 0){
            all.add(sb.toString());
        }
      }
      return all.toArray(new CharSequence[]{});
      
  }
    
  
  /**
   * 
   * @param addr1
   * @param addr2
   * @return a concatenation of addr1 and addr2
   */
  public static String toAddress(String addr1, String addr2){
      StringBuilder sb = new StringBuilder();
      if(!AppUtils.isEmpty(addr1)){
          sb.append(addr1).append(AppUtils.isBlank(addr2)?"":", ");
      }
      if(!AppUtils.isEmpty(addr2)){
          sb.append(addr2);
      }
      return sb.toString();
  }
  
  /**
   * @param city
   * @param state
   * @param zip
   * @return a concatenation of city, state, and zip
   */
  public static String toCszString(String city, String state, String zip){
      StringBuilder sb = new StringBuilder();
      if(!AppUtils.isEmpty(city)){
          sb.append(city).append((AppUtils.isEmpty(zip)&&AppUtils.isEmpty(state))? "": ", ");
      }

      if(!AppUtils.isEmpty(state)){
          sb.append(state);
      }
      if(!AppUtils.isEmpty(zip)){
          sb.append(" ").append(zip);
      }
      return sb.toString();
  }
  
  /**
   * @param editText
   * @return true if editText contains no text or only blank spaces
   * false otherwise
   */
  public static boolean isBlank(EditText editText){
      return editText.getText() == null || isBlank(editText.getText().toString());
  }
  
  /**
   * @param s
   * @return true if s is null or s.trim().length() == 0
   * false otherwise
   */
  public static boolean isBlank(String s){
      return s == null || s.trim().length() == 0;
  }
  
  /**
   * @param s
   * @return true if s is null or s.length() == 0
   * false otherwise
   */
  public static boolean isEmpty(String s){
      return s == null || s.length() == 0;
  }
}